from product import ptypes
global ptypes 

def is_valid(db,season_id,product_id,ptype):
	global ptypes
	cur=db.cursor()
	cur.execute("""select count(id) from seasons where id=%s and product_id=%s and p_type=%s""",(season_id,product_id,ptypes[ptype]))
	cnt=cur.fetchone()
	cur.close()
	if not cnt is None:
		return True
	else:
		return False

def get_id(db,season_name,ptype):
	global ptypes
	cur=db.cursor()
	cur.execute("""select id from seasons where name=%s and p_type=%s""", (season_name,ptypes[ptype]))
	id=cur.fetchone()
	cur.close()
	if not id is None : 
		return id[0]
	else:
		return -1

def insert_new(db,product_id,ptype,season):
	global ptypes
	cur=db.cursor()
	cur.execute("""insert into seasons (name,product_id,p_type)  VALUES (%s,%s,%s)""",(season,product_id,ptypes[ptype],))
	cur.execute("""select last_insert_id()""")
	id=cur.fetchone()[0]
	db.commit()
	cur.close()
	return id
